package com.elcl.activity.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.elcl.andbaselibrary.R;
import com.elcl.widget.comp.dialog.LoadingProgressDialog;
import com.elcl.activity.interfaces.BaseActivityMethods;
import com.elcl.network.OkHttp;
import com.elcl.storage.ApplicationCache;
import com.elcl.util.BaseDialogUtils;

public abstract class BaseFragmentActivity extends FragmentActivity implements
        BaseActivityMethods {
    protected LoadingProgressDialog dialog;
    protected static final String B2T = "animation_btmToUp";
    protected static final String R2L = "animation_rightToLeft";

    @SuppressLint("HandlerLeak")
    protected Handler netHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == OkHttp.IS_NET_FAILED) {
                fail();
            } else if (msg.what == OkHttp.RECOGNISE_FAILED) {//认证失败
                finish();
            } else {
                praseJsonMsg(msg);
            }
        }
    };

    private void praseJsonMsg(Message msg) {
        String json = "";
        if (null != msg.obj){
            json = msg.obj.toString();
        }
        praseJson(msg.what, json);
    }

    protected abstract void praseJson(int reqId, String json);

    protected void fail() {

    }

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        ApplicationCache.context = this;
    }

    @Override
    protected void onResume() {
        super.onResume();
        ApplicationCache.context = this;
    }

    /**
     * 界面中填充逐个填充textView的内容
     */
    @Override
    public void setText(int viewId, String str) {
        TextView view = (TextView) findViewById(viewId);
        if (null != str) {
            view.setText(str);
        } else {
            view.setText("");
        }
    }

    @Override
    public void setListener(int viewId, OnClickListener clickListener) {
        View view = findViewById(viewId);
        view.setOnClickListener(clickListener);
    }

    /**
     * 设置控件的可视性，一般和setText连用
     */
    @Override
    public void setVisibility(int viewId, int visibility) {
        View view = findViewById(viewId);
        view.setVisibility(visibility);
    }

    @Override
    public void showToast(String content) {
        Toast.makeText(ApplicationCache.context, content, Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void showToast(int resId) {
        Toast.makeText(ApplicationCache.context, resId, Toast.LENGTH_SHORT)
                .show();
    }

    @Override
    public void showLongToast(String content) {
        Toast.makeText(ApplicationCache.context, content, Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public void showLongToast(int resId) {
        Toast.makeText(ApplicationCache.context, resId, Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void openActivty(Class<?> cls) {
        startActivity(new Intent(this, cls));
    }

    @Override
    public void openActivty(Class<?> cls, String mid) {
        Intent intent = new Intent(ApplicationCache.context, cls);
        intent.putExtra("mid", mid);
        ApplicationCache.context.startActivity(intent);
    }

    @Override
    public void openActivty(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void openActivty(Class<?> cls, int[] animation) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
        overridePendingTransition(animation[0], animation[1]);
    }

    @Override
    public void openActivtyByViewId(int viewId, final Class<?> cls) {
        View view = findViewById(viewId);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivty(cls);
            }
        });
    }

    @Override
    public void openActivtyByViewId(int viewId, final Class<?> cls,
                                    final int[] animation) {
        View view = findViewById(viewId);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivty(cls, animation);
            }
        });
    }

    @Override
    public void setText(int[] viewId, String[] str) {
        int len = viewId.length;
        for (int i = 0; i < len; i++) {
            setText(viewId[i], str[i]);
        }
    }

    @Override
    public void setText(int viewId, String str, OnClickListener clickListener) {
        TextView view = (TextView) findViewById(viewId);
        view.setOnClickListener(clickListener);
        if (null != str) {
            view.setText(str);
        } else {
            view.setText("");
        }
    }

    @Override
    public void showProgressDialog(String... content) {
        BaseDialogUtils.showProgressDialog();
    }

    @Override
    public void openActivtyByAnim(Class<?> cls, String animationType) {
        Intent intent = new Intent(ApplicationCache.context, cls);
        startActivity(intent);
        if (animationType.equals(B2T)) {
            overridePendingTransition(R.anim.anim_btm2top, R.anim.anim_still);
        } else if (animationType.equals(R2L)) {
            overridePendingTransition(R.anim.anim_right_in, R.anim.anim_still);
        }
    }

    @Override
    public void dismissProgressDialog() {
        BaseDialogUtils.dismissProgressDialog();
    }

    @Override
    public void initBackBtn(int finishBtnId) {
        View finishBtn = findViewById(finishBtnId);
        finishBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
    }

}
